#include "bindings/bindings_bindable_artboard.h"
#include "helpers/general.h"
#include "rive/assets/file_asset.hpp"
#include <string>

namespace ohos_rive {
static rive::BindableArtboard *GetBindableArtboardFromNapiValue(napi_env env, napi_value value)
{
    napi_valuetype valueType;
    napi_typeof(env, value, &valueType);

    if (valueType != napi_external) {
        LOGE("Invalid napi value type, expected external");
        return nullptr;
    }

    void *data = nullptr;
    napi_status status = napi_get_value_external(env, value, &data);
    if (status != napi_ok || data == nullptr) {
        LOGE("Failed to get external value");
        return nullptr;
    }

    return reinterpret_cast<rive::BindableArtboard *>(data);
}

static napi_value CreateNapiValueFromPointer(napi_env env, void *pointer)
{
    napi_value externalValue;
    napi_status status = napi_create_external(env, pointer, nullptr, nullptr, &externalValue);
    if (status != napi_ok) {
        LOGE("Failed to create external napi value");
        return nullptr;
    }
    return externalValue;
}

/**
 * 清理 BindableArtboard 资源
 * 对应原 JNI 函数：Java_app_rive_runtime_kotlin_core_BindableArtboard_cppDelete
 */
napi_value BindableArtboardDelete(napi_env env, napi_callback_info info)
{
    size_t argc = 1;
    napi_value args[1];
    napi_value thisArg;
    void *data;

    napi_status status = napi_get_cb_info(env, info, &argc, args, &thisArg, &data);
    if (status != napi_ok || argc < 1) {
        LOGE("Failed to get callback info");
        return nullptr;
    }

    int64_t bindableArtboardPtr = 0;
    status = napi_get_value_int64(env, args[0], &bindableArtboardPtr);
    if (status != napi_ok) {
        LOGE("Failed to get BindableArtboard pointer from int64");
        return nullptr;
    }

    if (bindableArtboardPtr == 0) {
        LOGE("Invalid BindableArtboard pointer (0)");
        return nullptr;
    }

    rive::BindableArtboard *bindableArtboard = reinterpret_cast<rive::BindableArtboard *>(bindableArtboardPtr);
    LOGI("Deleting BindableArtboard at address: %{public}p", bindableArtboard);

    bindableArtboard->unref();
    napi_value result;
    napi_get_undefined(env, &result);
    return result;
}

/**
 * 获取 Artboard 名称
 * 对应原 JNI 函数：Java_app_rive_runtime_kotlin_core_BindableArtboard_cppName
 */
napi_value BindableArtboardGetName(napi_env env, napi_callback_info info)
{
    size_t argc = 1;
    napi_value args[1];
    napi_value thisArg;
    void *data;

    napi_status status = napi_get_cb_info(env, info, &argc, args, &thisArg, &data);
    if (status != napi_ok || argc < 1) {
        LOGE("Failed to get callback info");
        return nullptr;
    }

    int64_t bindableArtboardPtr = 0;
    status = napi_get_value_int64(env, args[0], &bindableArtboardPtr);
    if (status != napi_ok) {
        LOGE("Failed to get BindableArtboard pointer from int64");
        return nullptr;
    }

    if (bindableArtboardPtr == 0) {
        LOGE("Invalid BindableArtboard pointer (0)");
        return nullptr;
    }

    rive::BindableArtboard *bindableArtboard = reinterpret_cast<rive::BindableArtboard *>(bindableArtboardPtr);
    auto name = bindableArtboard->artboard()->name();
    LOGI("Get artboard name: %{public}s", name.c_str());

    napi_value result;
    status = napi_create_string_utf8(env, name.c_str(), NAPI_AUTO_LENGTH, &result);
    if (status != napi_ok) {
        LOGE("Failed to create string result");
        return nullptr;
    }

    return result;
}
} // namespace ohos_rive